unit uScreen;

interface

uses
  ExtCtrls,
  uTile, uObject, uMap;

type
  TGameScreen = class(TImage)
  private
    { Private declarations }
  public
    { Public declarations }
    useMap: TGameMap;
    procedure addTile(room: Integer; x, y: Integer; tile: TGameTile);
    procedure addObject(room: Integer; x: Integer; y: Integer);
    procedure showMap(room: Integer; x, y: Integer);
    procedure deleteTile(room: Integer; x, y: Integer);
    procedure deleteObject(room: Integer; x, y: Integer);
  end;

implementation

uses
  uMapEditor, Graphics, uObjectOptions;

procedure TGameScreen.addTile(room: Integer; x: Integer; y: Integer; tile: TGameTile);
begin
  useMap.rooms[room].tiles[x, y] := tile;
  if ((x - offsetX >= 0) and (x - offsetX < round(self.Width/TileSize)) and
     (y - offsetY >= 0) and (y - offsetY < round(self.Height/TileSize))) then
    begin
      self.Canvas.Draw((x - offsetX)*TileSize, (y - offsetY)*TileSize, useMap.rooms[room].tiles[x, y].tileSet.getTextureFrom(useMap.rooms[room].tiles[x, y].tileTexture));
      if ((useMap.rooms[room].objects[x, y].objectType <> '') and
         (useMap.rooms[room].objects[x, y].objectType <> 'not')) then
        begin
          self.Canvas.Draw((x - offsetX)*TileSize, (y - offsetY)*TileSize, useMap.rooms[room].objects[x, y].texture.getTextureFrom(useMap.rooms[room].objects[x, y].texturePosition));
          self.Canvas.Rectangle((x - offsetX)*TileSize + 2, (y - offsetY)*TileSize + 2, (x - offsetX)*TileSize + 6, (y - offsetY)*TileSize + 6);
        end;
    end;
end;

procedure TGameScreen.addObject(room: Integer; x: Integer; y: Integer);
begin
  GameObjectOptions.obj := useMap.rooms[room].objects[x, y];
  if (useMap.rooms[room].objects[x, y].objectType = '') then
    begin
      useMap.rooms[room].objects[x, y].objectType := 'something';
    end;
  if ((x - offsetX >= 0) and (x - offsetX < round(self.Width/TileSize)) and
     (y - offsetY >= 0) and (y - offsetY < round(self.Height/TileSize))) then
    begin
      if ((useMap.rooms[room].objects[x, y].objectType <> '') and
         (useMap.rooms[room].objects[x, y].objectType <> 'not')) then
        begin
          self.Canvas.Draw((x - offsetX)*TileSize, (y - offsetY)*TileSize, useMap.rooms[room].objects[x, y].texture.getTextureFrom(useMap.rooms[room].objects[x, y].texturePosition));
          self.Canvas.Rectangle((x - offsetX)*TileSize + 2, (y - offsetY)*TileSize + 2, (x - offsetX)*TileSize + 6, (y - offsetY)*TileSize + 6);
        end;
    end;
end;

procedure TGameScreen.showMap(room: Integer; x, y: Integer);
var
  i, j: Integer;
begin
  if (room <= high(useMap.rooms)) and (room >= low(useMap.rooms)) then
    begin
      for i := 0 to round(self.Width/TileSize) do
        begin
          for j := 0 to round(self.Height/TileSize) do
            begin
              if ((x+i >= low(useMap.rooms[room].tiles)) and
                 (x+i <= high(useMap.rooms[room].tiles))) then
                begin
                  if ((y+j >= low(useMap.rooms[room].tiles[x+i])) and
                     (y+j <= high(useMap.rooms[room].tiles[x+i]))) then
                    begin
                      self.Canvas.Draw(i*TileSize, j*TileSize, useMap.rooms[room].tiles[x+i, y+j].tileSet.getTextureFrom(useMap.rooms[room].tiles[x+i, y+j].tileTexture));
                    end;
                end;
              if ((x+i >= low(useMap.rooms[room].objects)) and
                 (x+i <= high(useMap.rooms[room].objects))) then
                begin
                  if ((y+j >= low(useMap.rooms[room].objects[x+i])) and
                     (y+j <= high(useMap.rooms[room].objects[x+i]))) then
                    begin
                      if ((useMap.rooms[room].objects[x+i, y+j].objectType <> '') and
                         (useMap.rooms[room].objects[x+i, y+j].objectType <> 'not')) then
                        begin
                          self.Canvas.Draw(i*TileSize, j*TileSize, useMap.rooms[room].objects[x+i, y+j].texture.getTextureFrom(useMap.rooms[room].objects[x+i, y+j].texturePosition));
                          self.Canvas.Rectangle(i*TileSize + 2, j*TileSize + 2, i*TileSize + 6, j*TileSize + 6);
                        end;
                    end;
                end;
            end;
        end;
    end;
end;

procedure TGameScreen.deleteTile(room: Integer; x: Integer; y: Integer);
begin
  useMap.rooms[room].tiles[x, y].isWater := false;
  useMap.rooms[room].tiles[x, y].isGorge := false;
  useMap.rooms[room].tiles[x, y].isGhosty := false;
  useMap.rooms[room].tiles[x, y].tileTexture := 0;
  useMap.rooms[room].tiles[x, y].transparent := false;
  useMap.rooms[room].tiles[x, y].tileSet := useMap.tileSets[0];
end;

procedure TGameScreen.deleteObject(room: Integer; x: Integer; y: Integer);
begin
  useMap.rooms[room].objects[x, y].objectType := 'nothing';
  useMap.rooms[room].objects[x, y].isLiving := false;
  useMap.rooms[room].objects[x, y].texture := useMap.tileSets[0];
  useMap.rooms[room].objects[x, y].texturePosition := 0;
end;

end.
